package com.jinke.uang.ui.activity.personal;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseNormalActivity;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.model.LoginModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.utils.StringUtils;
import com.jinke.uang.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 */
@Route(path = RouterMap.FIND_PASSWORD_PAGE)
public class FindPswActivity extends BaseNormalActivity <CommonPresenter,LoginModule> {

    @BindView(R.id.root_container_view)
    LinearLayout root_container_view;
    @BindView(R.id.et_userName)
    EditText etUserName;
    @BindView(R.id.et_code_verify)
    EditText etVerify;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.do_acquire_verify_code)
    TextView do_acquire_verify_code;

    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;

    private boolean isStartCountDown;
    private CountDownTimer countDownTimer;
    private String mPhoneNum;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topText.setText(R.string.forget_title);
    }

    @Override
    protected void onUIReady() {}

    @Override
    protected int getContentResId() {
        return R.layout.activity_find_psw;
    }

    @Override
    public String getPageTitle() {
        return null;
    }

    @Override
    protected int getResId() {
        return 0;
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected LoginModule getModule() {
        return new LoginModule();
    }

    @OnClick({R.id.confirm_btn, R.id.do_acquire_verify_code, R.id.root_container_view})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.do_acquire_verify_code:
                if (isStartCountDown) return;
                String hintMsg = getHintMsg();
                if (!StringUtils.isEmpty(hintMsg)) {
                    ToastUtils.showShort(this, hintMsg);
                    return;
                }
                mPresenter.getData(HttpConstant.SMS_CODE,null,etUserName.getText().toString());
                break;
            case R.id.confirm_btn:
                mPhoneNum = etUserName.getText().toString();
                String psw = etPassword.getText().toString();
                String verifyCode = etVerify.getText().toString();
                String hintMsg1 = checkUpdatePswParams();
                if (!StringUtils.isEmpty(hintMsg1)) {
                    ToastUtils.showShort(this, hintMsg1);
                    return;
                }
                mPresenter.getData(HttpConstant.REGISTER_QUEST,null, mPhoneNum, psw, verifyCode, "2");
                break;
            case R.id.root_container_view:
                hideSoftPan(root_container_view);
                break;
            default:
                break;
        }
    }

    public String getHintMsg() {
        if (etUserName == null || etUserName.getText() == null || StringUtils.isEmpty(etUserName
                .getText().toString()))
            return getStringResource(R.string.login_type_user_name);
        return "";
    }

    private String checkUpdatePswParams() {
        if (etUserName == null || etUserName.getText() == null || StringUtils.isEmpty(etUserName
                .getText().toString())) {
            return getStringResource(R.string.register_user_name_is_null);
        }
        if (etVerify == null || etVerify.getText() == null || StringUtils.isEmpty(etVerify
                .getText().toString())) {
            return getStringResource(R.string.register_verify_code_is_null);
        }
        if (etPassword == null || etPassword.getText() == null || StringUtils.isEmpty(etPassword
                .getText().toString())) {
            return getStringResource(R.string.register_psw_is_null);
        }
        return null;
    }

    private void processCountDownView() {
        isStartCountDown = true;
        if (countDownTimer == null) {
            countDownTimer = new CountDownTimer(59 * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    do_acquire_verify_code.setText((millisUntilFinished / 1000 + 1) + "s");
                }

                @Override
                public void onFinish() {
                    isStartCountDown = false;
                    do_acquire_verify_code.setText(getStringResource(R.string
                            .register_acquire_verity_code));
                }
            };
        } else {
            countDownTimer.cancel();
        }
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    @Override
    public void showResponse(int apiType,String listType, Object o) {
        //忘记密码时可以通过注册和短信验证重新登录
        if(apiType== HttpConstant.REGISTER_QUEST){ //注册接口 type为2
            BaseResponse<String> baseResponse = (BaseResponse<String>) o;
            if (baseResponse.getStatus() == Constant.HTTP_OK) {
                //跳转成功页
                ARouter.getInstance().build(RouterMap.REGISTER_RESULT).withString(LoginActivity.USER_NAME,
                        mPhoneNum.trim()).withInt("result_type",2).navigation();
            } else {
                if (!StringUtils.isEmpty(baseResponse.getMessage())) {
                    ToastUtils.showShort(FindPswActivity.this, baseResponse.getMessage());
                }
            }
        }

        else if(apiType==HttpConstant.SMS_CODE){//短信验证接口
            BaseResponse<String> baseResponse = (BaseResponse<String>) o;
            if (baseResponse.getStatus() == Constant.HTTP_OK) {
                processCountDownView();
            } else {
                if (StringUtils.isEmpty(baseResponse.getMessage())) {
                    ToastUtils.showShort(FindPswActivity.this, baseResponse.getMessage());
                }
            }
        }
    }

    @Override
    public void showError(Throwable e) {
        dismissLoadingView();
    }
}
